package com.moxiao.yfjh.module.maker.service.memberStringing;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.module.maker.controller.admin.memberStringing.vo.MatchmakerMemberStringingCreateReqVO;
import com.moxiao.yfjh.module.maker.controller.admin.memberStringing.vo.MatchmakerMemberStringingExportReqVO;
import com.moxiao.yfjh.module.maker.controller.admin.memberStringing.vo.MatchmakerMemberStringingPageReqVO;
import com.moxiao.yfjh.module.maker.controller.admin.memberStringing.vo.MatchmakerMemberStringingUpdateReqVO;
import com.moxiao.yfjh.module.maker.convert.memberStringing.MatchmakerMemberStringingConvert;
import com.moxiao.yfjh.module.maker.dal.dataobject.memberStringing.MatchmakerMemberStringingDO;
import com.moxiao.yfjh.module.maker.dal.mysql.memberStringing.MatchmakerMemberStringingMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.maker.enums.ErrorCodeConstants.MATCHMAKER_MEMBER_STRINGING_NOT_EXISTS;

/**
 * 相亲红娘会员用户牵线总次数 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class MatchmakerMemberStringingServiceImpl implements MatchmakerMemberStringingService {

    @Resource
    private MatchmakerMemberStringingMapper matchmakerMemberStringingMapper;

    @Override
    public Long createMatchmakerMemberStringing(MatchmakerMemberStringingCreateReqVO createReqVO) {
        // 插入
        MatchmakerMemberStringingDO matchmakerMemberStringing = MatchmakerMemberStringingConvert.INSTANCE.convert(createReqVO);
        matchmakerMemberStringingMapper.insert(matchmakerMemberStringing);
        // 返回
        return matchmakerMemberStringing.getId();
    }

    @Override
    public void updateMatchmakerMemberStringing(MatchmakerMemberStringingUpdateReqVO updateReqVO) {
        // 校验存在
        validateMatchmakerMemberStringingExists(updateReqVO.getId());
        // 更新
        MatchmakerMemberStringingDO updateObj = MatchmakerMemberStringingConvert.INSTANCE.convert(updateReqVO);
        matchmakerMemberStringingMapper.updateById(updateObj);
    }

    @Override
    public void deleteMatchmakerMemberStringing(Long id) {
        // 校验存在
        validateMatchmakerMemberStringingExists(id);
        // 删除
        matchmakerMemberStringingMapper.deleteById(id);
    }

    private void validateMatchmakerMemberStringingExists(Long id) {
        if (matchmakerMemberStringingMapper.selectById(id) == null) {
            throw exception(MATCHMAKER_MEMBER_STRINGING_NOT_EXISTS);
        }
    }

    @Override
    public MatchmakerMemberStringingDO getMatchmakerMemberStringing(Long id) {
        return matchmakerMemberStringingMapper.selectById(id);
    }

    @Override
    public List<MatchmakerMemberStringingDO> getMatchmakerMemberStringingList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return matchmakerMemberStringingMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<MatchmakerMemberStringingDO> getMatchmakerMemberStringingPage(
            MatchmakerMemberStringingPageReqVO pageReqVO) {
        return matchmakerMemberStringingMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MatchmakerMemberStringingDO> getMatchmakerMemberStringingList(
            MatchmakerMemberStringingExportReqVO exportReqVO) {
        return matchmakerMemberStringingMapper.selectList(exportReqVO);
    }

    @Override
    public MatchmakerMemberStringingDO getLastNo(Date todayStart, Date todayEnd) {
        LambdaQueryWrapper<MatchmakerMemberStringingDO> lqw = Wrappers.lambdaQuery();
        if (Objects.nonNull(todayStart) && Objects.nonNull(todayEnd)) {
            lqw.between(MatchmakerMemberStringingDO::getCreateTime, todayStart, todayEnd);
        }
        lqw.orderByDesc(MatchmakerMemberStringingDO::getCreateTime);
        lqw.last("LIMIT " + 1);
        return matchmakerMemberStringingMapper.selectOne(lqw);
    }

    @Override
    public Integer repeatChecking(String code) {
        LambdaQueryWrapper<MatchmakerMemberStringingDO> lqw = Wrappers.lambdaQuery();
        lqw.eq(true, MatchmakerMemberStringingDO::getNo, code);
        List<MatchmakerMemberStringingDO> list = matchmakerMemberStringingMapper.selectList(lqw);
        return list.size();
    }

    @Override
    public MatchmakerMemberStringingDO getSelectOne(Long userId) {
        LambdaQueryWrapper<MatchmakerMemberStringingDO> lqw = Wrappers.lambdaQuery();
        lqw.eq(MatchmakerMemberStringingDO::getMemberId, userId);
        lqw.last("LIMIT " + 1);
        return matchmakerMemberStringingMapper.selectOne(lqw);
    }

    @Override
    public void updateNum(Long id, Integer num) {
        matchmakerMemberStringingMapper.update(null, new LambdaUpdateWrapper<MatchmakerMemberStringingDO>()
                .set(MatchmakerMemberStringingDO::getStringingNum, num)
                .eq(MatchmakerMemberStringingDO::getId, id));
    }
}
